import tkinter as tk
from tkinter import simpledialog
import random


# 欲望初始化和顺序
plans = [
    {'欲望': '跑步', '强度': '1'},
    {'欲望': '睡觉', '强度': '10'},
    {'欲望': '吃饭', '强度': '10'}
]

# 吃饭的欲望
def eat_sort():
    global 吃饭_value
    name = '吃饭'
    num = random.randint(1, 5)
    if random.random() > 0.75:
        吃饭_value.set(吃饭_value.get() + num)
    else:
        吃饭_value.set(吃饭_value.get() - num)
        bubble_need(name)
    print(name)
    # 跑步时间间隔：0~10秒
    root.after(random.randint(0, 10000), eat_sort)

# 跑步的欲望
def move_sort():
    global 跑步_value
    name = '跑步'
    num = random.randint(1, 5)
    if random.random() > 0.6:
        # 增加
        跑步_value.set(跑步_value.get() + num)
    else:
        # 减少
        跑步_value.set(跑步_value.get() - num)
        bubble_need(name)
    print(name)
    # 跑步时间间隔0~50秒
    root.after(random.randint(0, 50000), move_sort)

# 睡觉的欲望
def sleep_sort():
    global 睡觉_value
    name = '睡觉'
    num = random.randint(1, 5)
    if random.random() > 0.5:
        # 增加
        睡觉_value.set(睡觉_value.get() + num)
    else:
        # 减少
        睡觉_value.set(睡觉_value.get() - num)
        bubble_need(name)
    print(name)
    # 睡眠时间间隔0~24秒
    root.after(random.randint(0, 24000), sleep_sort)

# 执行的效率
def bubble_need(name):
    global plans
    index = -1
    for i in range(len(plans) - 1, -1, -1):
        if plans[i]['欲望'] == name:
            # 找到索引位 并且 重新赋值
            index = i
            plans[i]['强度'] = int(eval(name + '_value.get()'))
        if index > 0 and i > 0:
            if int(plans[i]['强度']) < int(plans[i - 1]['强度']):
                # 显示对比项
                print(plans[i], plans[i - 1])
                # 交换位置
                plans[i], plans[i - 1] = plans[i - 1], plans[i]

    # 显示最新排序
    html = ''
    for plan in plans:
        html += f'[{plan["欲望"]}]进度: {plan["强度"]} '
    box_label.config(text=html)
    
# 找到最小值
def need_run():
    global plans
    min_plan = plans[0]
    val = simpledialog.askstring('满足欲望', f'满足{min_plan["欲望"]}', initialvalue='1')
    if val is not None:
        obj = eval(min_plan['欲望'] + '_value')
        num = int(obj.get()) + int(val)
        plans[0]['强度'] = num
        obj.set(num)

root = tk.Tk()
root.title('欲望的提升')
root.geometry('400x300')


# 吃饭
f1 = tk.Frame(root)
f1.pack()
吃饭_value = tk.IntVar(value=10)
tk.Label(f1, text='吃饭:').pack(side="left")
tk.Entry(f1, textvariable=吃饭_value).pack(side="right")
# 跑步
f2 = tk.Frame(root)
f2.pack()
跑步_value = tk.IntVar(value=1)
tk.Label(f2, text='跑步:').pack(side="left")
tk.Entry(f2, textvariable=跑步_value).pack(side="right")
# 睡觉
f3 = tk.Frame(root)
f3.pack()
睡觉_value = tk.IntVar(value=10)
tk.Label(f3, text='睡觉:').pack(side="left")
tk.Entry(f3, textvariable=睡觉_value).pack(side="right")
# 按钮
tk.Button(root, text='满足欲望', command=need_run).pack()

# 结果显示区
box_label = tk.Label(root, text='', wraplength=380)
box_label.pack()


root.after(0, eat_sort)
root.after(0, move_sort)
root.after(0, sleep_sort)

root.mainloop()